package com.orange.adminapi.controller.category;

import com.fasterxml.jackson.annotation.JsonView;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.category.ProductCategory;
import com.orange.core.service.category.ProductCategoryService;
import com.orange.core.validator.group.Update;
import com.orange.core.validator.view.Simple;
import com.orange.core.validator.view.SimpleWithId;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;

    /**
     * 列表
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).PRODUCT_CATEGORY_LIST.permission.identifier)")
    @GetMapping("/list")
    public R<List<ProductCategory>> list() {
        return R.ok(productCategoryService.list());
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).PRODUCT_CATEGORY_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated @JsonView(Simple.class) ProductCategory productCategory) {
        return R.ok(productCategoryService.save(productCategory));
    }

    /**
     * 编辑
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).PRODUCT_CATEGORY_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated(Update.class) @JsonView(SimpleWithId.class) ProductCategory productCategory) {
        return R.ok(productCategoryService.updateById(productCategory));
    }

    /**
     * 删除
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).PRODUCT_CATEGORY_DELETE.permission.identifier)")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable int id) {
        return R.ok(productCategoryService.del(id));
    }
}
